package com.swust;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

public class MybatisDemo01 {
        @Test
        public void testfindBySno() {
            //1.构建一个输入流,读取sqlMapConfig.xml文件
            InputStream is = MybatisDemo01.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");

            //2.构建一个sqlSessionFactory工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //3.通过sqlSessionFactory工厂获取一个sqlSession会话接口
            SqlSession sqlSession = sqlSessionFactory.openSession();

            //4.调用sqlSession方法进行CRUD操作,第一个参数传入statement id值
            //我们一般传入 命名空间.id
            Student student = sqlSession.selectOne("test.findBySno", 201215121);
            System.out.println(student);
            //5.关闭会话
            sqlSession.close();
        }
        @Test
        public void testfindBysname2() {
            //1.构建一个输入流,读取sqlMapConfig.xml文件
            InputStream is= MybatisDemo01.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");
            //2.构建一个sqlSessionFactory工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过sqlSessionFactory工厂获取一个sqlSession会话接口
            SqlSession sqlSession = sqlSessionFactory.openSession();
            sqlSession.selectList("test.findBySname","李").forEach(System.out::println);
            sqlSession.close();
        }
        @Test
        public void testdeletestudent() {
            InputStream is=MybatisDemo01.class.getClassLoader().getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            sqlSession.delete("test.deleteStudent",201215127);
            sqlSession.commit();
        }
}
